
package com.kugou.game.sdk.ui.widget;

import com.kugou.game.sdk.utils.Rx;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * 推荐用户名的弹出框
 * 
 * @author liaodongmingRabbit
 */
public class RecomendUsernamePopWindow extends KGPopupWindow {

    private ArrayList<String> mRecommendUsernames = null;

    private Context mContext = null;

    private LayoutInflater mInflater = null;

    private ListView mPayRecommendUsernameList = null;

    private ImageView mPayClosePopupWindow = null;

    private OnClickListener mClickListener = null;

    private RecommendUsernameAdapter mAdapter = null;

    public RecomendUsernamePopWindow(Activity context, ArrayList<String> recommendUsernames,
            OnClickListener onClickListener) {
        super(context);
        this.mRecommendUsernames = recommendUsernames;
        this.mContext = context;
        this.mClickListener = onClickListener;
        if (context instanceof Activity) {
            mInflater = ((Activity) context).getLayoutInflater();
        } else {
            mInflater = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
        }
        final View v = mInflater.inflate(Rx.layout.kg_recommend_username_popwindow, null);
        setContentView(v);
        setupView();
    }

    private void setupView() {
        mPayClosePopupWindow = (ImageView) findViewById(Rx.id.kg_close_popup_window);
        mPayClosePopupWindow.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        mPayRecommendUsernameList = (ListView) findViewById(Rx.id.kg_recommend_username_list);
        mAdapter = new RecommendUsernameAdapter(mRecommendUsernames);
        mPayRecommendUsernameList.setCacheColorHint(Color.TRANSPARENT);
        mPayRecommendUsernameList.setAdapter(mAdapter);
        mAdapter.notifyDataSetInvalidated();
    }

    @Override
    protected void populateActionItems(ArrayList<ActionItem> actionItems) {

    }

    @Override
    protected void onMeasureAndLayout(Rect anchorRect, View contentView) {
        setWidgetSpecs(getScreenHeight() - anchorRect.top, false);
    }

    @Override
    protected void showArrow() {

    }

    @Override
    protected void prepareAnimationStyle() {

    }

    @Override
    protected void onShow() {

    }

    @Override
    protected void onDismiss() {

    }

    class RecommendUsernameAdapter extends BaseAdapter {

        private ArrayList<String> mRecommendUsernames = null;

        public RecommendUsernameAdapter(ArrayList<String> recommendUsernames) {
            super();
            this.mRecommendUsernames = recommendUsernames;

        }

        @Override
        public int getCount() {
            return mRecommendUsernames.size();
        }

        @Override
        public Object getItem(int position) {
            return mRecommendUsernames.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            MyCache cache;
            if (convertView == null) {
                final LayoutInflater inflater = (LayoutInflater) mContext
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(Rx.layout.kg_recommend_username_item, null);
                cache = new MyCache();
                cache.nameText = (TextView) convertView.findViewById(Rx.id.kg_recommend_name_text);
                cache.nameButton = (Button) convertView
                        .findViewById(Rx.id.kg_recommend_name_button);
                cache.nameButton.setOnClickListener(mClickListener);
                convertView.setTag(cache);
            } else {
                cache = (MyCache) convertView.getTag();
            }
            if (mRecommendUsernames != null) {
                cache.nameText.setText(mRecommendUsernames.get(position));
                cache.nameButton.setTag(position);
            }

            return convertView;
        }

        class MyCache {
            TextView nameText;

            Button nameButton;
        }
    }
}
